﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilZ.Dotnet.Ex.Attributes;
using UtilZ.Dotnet.Ex.ValueDescription;

namespace UtilZ.Dotnet.Ex.Communication.Net.Telnet.Commands
{
    [ValueDescriptionGroupAttribute("命令")]
    internal class TelnetCommandConstant
    {

        /// <summary>
        /// 设置
        /// </summary>
        [DisplayNameExAttribute("设置(tset -encoding utf-8)")]
        public const string TSET = "tset";

        /// <summary>
        /// 帮助
        /// </summary>
        [DisplayNameExAttribute("帮助")]
        public const string HELP = "-h";

        /// <summary>
        /// 退出
        /// </summary>
        [DisplayNameExAttribute("退出")]
        public const string EXIT = "-q";

        /// <summary>
        /// 获取Telnet信息
        /// </summary>
        [DisplayNameExAttribute("查看Telnet信息")]
        public const string GET_TELNET_INFO = "-info";

        /// <summary>
        /// 设置编码
        /// </summary>
        [DisplayNameExAttribute("设置编码")]
        public const string ENCODING = "-e";

        /// <summary>
        /// 清空屏幕命令
        /// </summary>
        [DisplayNameExAttribute("清空屏幕命令","未实现,未找着相应的命令")]
        public const string CLEAR_SCREEN_COMMAND = "-cs";

        /// <summary>
        /// 清空历史命令
        /// </summary>
        [DisplayNameExAttribute("清空历史命令")]
        public const string CLEAR_HISTORY_COMMAND = "-c";

        /// <summary>
        /// 设置历史命令缓存大小
        /// </summary>
        [DisplayNameExAttribute("设置历史命令缓存大小","小于等于0无限,大于0仅缓存设置值")]
        public const string SET_HISTORY_COMMAND_CAPCITY = "-hcc";
        

        /// <summary>
        /// 指定终端交互系统
        /// </summary>
        [DisplayNameExAttribute("指定终端交互系统","2:windowsNT;4:linux/unix;6:mac os")]
        public const string TERMINAL_OS_COMMAND = "-tos";






        private readonly static ValueDescriptionGroup<string> _group = null;
        static TelnetCommandConstant()
        {
            _group = new ValueDescriptionGroup<string>(typeof(TelnetCommandConstant), "telnet命令");
        }

        /// <summary>
        /// 获取命令帮助信息
        /// </summary>
        /// <returns></returns>
        public static string GetCommandHelpInfo()
        {
            int nameMaxLength = 0, desMaxLength = 0;
            foreach (var item in _group)
            {
                if (item.DisplayName.Length > nameMaxLength)
                {
                    nameMaxLength = item.DisplayName.Length;
                }

                if (item.Description != null && item.Description.Length > desMaxLength)
                {
                    desMaxLength = item.Description.Length;
                }
            }

            const int SPACE = 10;
            int nameLen = nameMaxLength + SPACE;
            int desLen = desMaxLength + SPACE;
            string cmd, des;

            StringBuilder sb = new StringBuilder();

            sb.Append(TSET);
            sb.AppendLine("命令说明");

            cmd = $"{{0, -{nameLen - 2}}}";
            sb.Append(string.Format(cmd, "命令"));

            des = $"{{0, -{desLen}}}";
            sb.AppendLine(string.Format(des, "描述"));


            foreach (var item in _group)
            {
                cmd = $"{{0, -{nameLen}}}";
                sb.Append(string.Format(cmd, item.Value));

                des = $"{{0, -{0}}}";
                sb.Append(string.Format(des, item.DisplayName));
                if (!string.IsNullOrWhiteSpace(item.Description))
                {
                    sb.Append("(");
                    sb.Append(item.Description);
                    sb.Append(")");
                }
                sb.AppendLine();
            }

            return sb.ToString();
        }
    }
}
